package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.service.WebUserService;
import com.jt.utils.CookieUtil;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller     //需要进行页面跳转
@RequestMapping("/user")
public class UserController {
    @Autowired
    WebUserService userService;

    @Reference(check = false)
    DubboUserService dubboUserService;
    @Autowired
    JedisCluster jedisCluster;
    /**
     * 实现用户的退出操作.重定向到系统首页
     * url: http://www.jt.com/user/logout.html
     * 业务:
     * 1.删除Redis中的数据  key
     * 2.删除Cookie记录
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        //1.根据JT_TICKET获取指定的ticket
        String ticket = CookieUtil.getCookieValue(request, "JT_TICKET");
        //2.判断ticket是否为null
        if (!StringUtils.isEmpty(ticket)) {
            jedisCluster.del(ticket);
            CookieUtil.deleteCookie(response, "JT_TICKET", "jt.com");
        }
        return "redirect:/";
    }

    // 用户登录功能
    @RequestMapping("/doLogin")
    @ResponseBody
    public SysResult doLogin(User user, HttpServletResponse response) {
        String ticket = dubboUserService.doLogin(user);
        if (StringUtils.isEmpty(ticket))
            return SysResult.fail();
        Cookie cookie = new Cookie("JT_TICKET", ticket);
        cookie.setMaxAge(7 * 24 * 60 * 60);   //设定cookie存活有效期
        cookie.setPath("/");            //设定cookie有效范围
        cookie.setDomain("jt.com");     //设定cookie共享的域名 是实现单点登录必备要素
        response.addCookie(cookie);
        return SysResult.success();
    }

    // 用户注册功能
    @RequestMapping("/doRegister")
    @ResponseBody
    public SysResult doRegister(User user) {
        dubboUserService.saveUser(user);
        return SysResult.success();
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public List<User> testHttpClient() {
//        return userService.findAll();
        return dubboUserService.findAll();

    }

    /**
     * 实现用户模块页面跳转
     * url1: http://www.jt.com/user/login.html     页面:login.jsp
     * url2: http://www.jt.com/user/register.html  页面:register.jsp
     * 要求:实现通用页面跳转
     * restFul方式: 1.动态获取url中的参数,之后实现通用的跳转.
     */
    @RequestMapping("/{moduleName}")
    public String module(@PathVariable String moduleName) {

        return moduleName;
    }

}